package com.example.traning.mall.controller.account;

import com.example.traning.mall.controller.account.vo.*;
import com.example.traning.mall.framework.common.pojo.PageParam;
import com.example.traning.mall.framework.common.pojo.R;
import com.example.traning.mall.service.account.IAccountUserService;
import com.github.xingfudeshi.knife4j.annotations.Ignore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author Lei Yu
 * @since 2025-04-29
 */
@Tag(name = "用户")
@RequiredArgsConstructor
@RestController

public class AccountUserController {

    private final IAccountUserService userService;

    // ================ auth user ================

    @Operation(summary = "2. 单点登录 - 用户登录")
    @RequestMapping(value = "/passport/login", method = RequestMethod.POST)
    @PermitAll
    public R login(UserLoginReqVO reqVO) {
        return R.ok(userService.login(reqVO));
    }

    @PostMapping("/passport/logout")
    @Operation(summary = "2. 单点登录 - 退出登录")
    public R logout(@AuthenticationPrincipal @Ignore CurrentPrincipal currentPrincipal) {
        userService.logout(currentPrincipal);
        return R.ok();
    }

    // ================ users 增删改查 ================

    @PostMapping("/users/add-new")
    @PreAuthorize("hasAuthority('/account/user/add-new')")
    @Operation(summary = "添加用户")
    public R addNew(@Valid UserAddNewParam userAddNewParam) {
        userService.addNew(userAddNewParam);
        return R.ok();
    }

    @PostMapping("/users/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/account/user/delete')")
    @Operation(summary = "根据ID删除用户")
    @Parameter(name = "id", description = "用户ID", required = true)
    public R delete(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id) {
        userService.delete(id);
        return R.ok();
    }

    @PostMapping("/users/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/account/user/edit')")
    @Operation(summary = "修改基本信息")
    @Parameter(name = "id", description = "用户ID", required = true)
    public R updateInfo(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                           @Valid UserUpdateInfoParam userUpdateInfoParam) {
        userService.updateInfo(id, userUpdateInfoParam);
        return R.ok();
    }

    @Operation(summary = "1. 用户管理 - 根据ID查询用户")
    @Parameter(name = "id", description = "用户ID", required = true)
    @GetMapping("/users/{id}")
    @PreAuthorize("hasAuthority('/account/user/query')")
    public R getStandardById(@PathVariable
                                             @Min(value = 1, message = "请提交合法的用户ID值：id>=1") Long id) {
        UserStandardVO tag = userService.getStandardById(id);
        return R.ok(tag);
    }

    @GetMapping("/users")
    @PreAuthorize("hasAuthority('/account/user/query')")
    @Operation(summary = "查询用户列表")
    public R list(PageParam param) {
        return R.ok(userService.selectPage(param));
    }

}
